﻿Imports System.ComponentModel
Imports LSW.Win32

Namespace QQ
    <ToolboxBitmap(GetType(TextBox))> _
    Public Class QQTextBoxBase
        Inherits TextBox
#Region "变量"
        ''' <summary>
        ''' 水印文字
        ''' </summary>
        Private _waterText As String = String.Empty
        ''' <summary>
        ''' 水印文字的颜色
        ''' </summary>
        Private _waterColor As Color = Color.DarkGray
#End Region

#Region "属性"
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("水印文字"), Category("自定义属性")> _
        Public Property WaterText() As String
            Get
                Return Me._waterText
            End Get
            Set(value As String)
                Me._waterText = value
                MyBase.Invalidate()
            End Set
        End Property
        ''' <summary>
        ''' 
        ''' </summary>
        <Description("水印的颜色"), Category("自定义属性")> _
        Public Property WaterColor() As Color
            Get
                Return Me._waterColor
            End Get
            Set(value As Color)
                Me._waterColor = value
                MyBase.Invalidate()
            End Set
        End Property
#End Region

#Region "方法"
        ''' <summary>
        ''' 绘制水印
        ''' </summary>
        Private Sub WmPaintWater(ByRef m As Message)
            Using g As Graphics = Graphics.FromHwnd(MyBase.Handle)
                If Me.Text.Length = 0 AndAlso Not String.IsNullOrEmpty(Me._waterText) AndAlso Not Me.Focused Then
                    Dim flags As TextFormatFlags = TextFormatFlags.EndEllipsis Or TextFormatFlags.VerticalCenter
                    If Me.RightToLeft = System.Windows.Forms.RightToLeft.Yes Then
                        flags = flags Or TextFormatFlags.RightToLeft Or TextFormatFlags.Right
                    End If
                    TextRenderer.DrawText(g, Me._waterText, New Font("宋体", 8.5F), Me.ClientRectangle, Me._waterColor, flags)
                End If
            End Using
        End Sub
#End Region

#Region "Override Methods"
        ''' <summary>
        ''' 
        ''' </summary>
        ''' <param name="m"></param>
        Protected Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
            If m.Msg = CInt(WindowsMessage.WM_PAINT) Then
                Me.WmPaintWater(m)
            End If
            '绘制水印
        End Sub
#End Region
    End Class

End Namespace